﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using System.Data;

namespace T.FAS.Runtime.Base.Dao
{
    public class FASAuthResultDao : DaoService<FASAuthResult>
    {
        public override string TableName => "FAS_AuthResult";
        public override string IDColumnName => "ID";
        private string ForeignKeyName => "SubSystemID";
        public override List<string> ExceptColumnName => ExceptColumnNames;

        private List<string> _excepColumnName;
        protected List<string> ExceptColumnNames
        {
            get
            {
                if (_excepColumnName == null)
                    _excepColumnName = DaoUtil.GetExceptionColumn<FASAuthResult>();
                return _excepColumnName;
            }
        }

        public FASAuthResultDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="queryParam"></param>
        /// <returns></returns>
        public bool AuthUser(AuthServiceParam queryParam)
        {
            if (queryParam == null)
            {
                return false;
            }
            if (queryParam.BusinessType == AuthBusinessType.None)
                return false;
            string filterSQL = null;
            if (!string.IsNullOrEmpty(queryParam.SubSystemCode))
                filterSQL = $" AND SubSystemCode=@SubSystemCode ";
            if (filterSQL == null) return false;
            filterSQL = $" BusinessType={(int)queryParam.BusinessType} AND UserID=@UserID" + filterSQL;
            List<IDbDataParameter> param = new List<IDbDataParameter>
            {
                DB.MakeInParam("@SubSystemCode", queryParam.SubSystemCode),
                DB.MakeInParam("@UserID", queryParam.UserID),
            };
            var dt = DB.ExecuteDataSet($"select * from {TableName} where {filterSQL}", param.ToArray())?.Tables?[0];
            var result = DaoUtil.ConvertToEntity<FASAuthResult>(dt, this.ExceptColumnName);
            return result != null && result.Count > 0;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="queryParam"></param>
        /// <returns></returns>
        public List<FASAuthResult> GetFASAuthResultByFilter(AuthServiceParam queryParam)
        {
            if (queryParam == null)
            {
                return default;
            }
            if (queryParam.BusinessType == AuthBusinessType.None)
                return default;
            string filterSQL = null;
            switch (queryParam.Dimension)
            {
                case AuthDimensionType.ByUserID:
                    if (!string.IsNullOrEmpty(queryParam.UserID))
                        filterSQL = $" AND UserID=@UserID ";
                    break;
                case AuthDimensionType.BySubSystem:
                    if (!string.IsNullOrEmpty(queryParam.SubSystemID))
                        filterSQL = $" AND SubSystemID=@SubSystemID ";
                    if (!string.IsNullOrEmpty(queryParam.SubSystemCode))
                        filterSQL = $" AND SubSystemCode=@SubSystemCode ";
                    break;
                case AuthDimensionType.ByModule:
                    if (!string.IsNullOrEmpty(queryParam.SubSystemID) && !string.IsNullOrEmpty(queryParam.ModuleID))
                        filterSQL = $" AND SubSystemID=@SubSystemID and ModuleID=@ModuleID ";
                    break;
                default:
                    break;
            }
            if (filterSQL == null) return default;
            filterSQL = $" BusinessType={(int)queryParam.BusinessType} " + filterSQL;

            List<IDbDataParameter> param = new List<IDbDataParameter>();
            if (!string.IsNullOrEmpty(queryParam.UserID))
                param.Add(DB.MakeInParam("@UserID", queryParam.UserID));
            if (!string.IsNullOrEmpty(queryParam.SubSystemID))
                param.Add(DB.MakeInParam("@SubSystemID", queryParam.SubSystemID));
            if (!string.IsNullOrEmpty(queryParam.SubSystemCode))
                param.Add(DB.MakeInParam("@SubSystemCode", queryParam.SubSystemCode));
            if (!string.IsNullOrEmpty(queryParam.ModuleID))
                param.Add(DB.MakeInParam("@ModuleID", queryParam.ModuleID));

            var dt = DB.ExecuteDataSet($"select * from {TableName} where {filterSQL}", param.ToArray())?.Tables?[0];
            return DaoUtil.ConvertToEntity<FASAuthResult>(dt, this.ExceptColumnName);
        }
         
        /// <summary>
        /// 添加FASAuthResult
        /// </summary>
        /// <param name="authResult"></param>
        public void BatchCreateFASAuthResults(List<FASAuthResult> authResult)
        {
            try
            {
                DaoUtil.ExecuteInsertSQL(authResult, this);
            }
            catch (Exception e)
            {
                throw new FasException("", e.Message, e);
            }
        }

        /// <summary>
        /// 删除FASAuthResult
        /// </summary>
        /// <param name="serviceParam"></param>
        public void DeleteFASAuthResultByFilter(AuthServiceParam serviceParam)
        {
            if (serviceParam == null) return;
            try
            {
                if (serviceParam.BusinessType == AuthBusinessType.None)
                    return;
                string filterSQL = null;
                switch (serviceParam.Dimension)
                {
                    case AuthDimensionType.ByUserID:
                        if (!string.IsNullOrEmpty(serviceParam.UserID))
                            filterSQL = $" UserID=@UserID ";
                        break;
                    case AuthDimensionType.BySubSystem:
                        if (!string.IsNullOrEmpty(serviceParam.SubSystemID))
                            filterSQL = $" SubSystemID=@SubSystemID ";
                        break;
                    case AuthDimensionType.ByModule:
                        if (!string.IsNullOrEmpty(serviceParam.SubSystemID) && !string.IsNullOrEmpty(serviceParam.ModuleID))
                            filterSQL = $" SubSystemID=@SubSystemID and ModuleID=@ModuleID ";
                        break;
                    default:
                        break;
                }
                if (filterSQL == null) return;
                List<IDbDataParameter> param = new List<IDbDataParameter>
                {
                    DB.MakeInParam("@SubSystemID", serviceParam.SubSystemID),
                    DB.MakeInParam("@ModuleID", serviceParam.ModuleID),
                    DB.MakeInParam("@UserID", serviceParam.UserID),
                };
                filterSQL = $" BusinessType={(int)serviceParam.BusinessType} and " + filterSQL;
                var delSQL = "DELETE FROM {0} WHERE {1}";
                string delSql = string.Format(delSQL, TableName, filterSQL);
                DB.ExecSqlStatement(delSql, param.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException("", e.Message, e);
            }
        } 
    }
}
